package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.behavior.service.ApCollectionBehaviorService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.behavior.service.ApUnlikeBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.feigns.UserFeign;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.dtos.LikesBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.constants.user.UserRelationConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {

    @Autowired
    ApBehaviorEntryServiceImpl apBehaviorEntryService;

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    UserFeign userFeign;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {

        //1. 判断用户是否登录
        ApUser user = AppThreadLocalUtils.getUser();
        Map map = new HashMap<>();
        map.put("islike", false);
        map.put("isfollow", false);
        map.put("isunlike", false);
        map.put("iscollection", false);
        if (user == null) {
            //2. 未登录 直接将 4种行为 均返回false
            return ResponseResult.okResult(map);
        }
        //3. 如果登录
        //4. 查询行为实体
        ApBehaviorEntry entry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        if (entry == null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "行为实体不存在");
        }


        Query apLikes = Query.query(Criteria.where("articleId").is(dto.getArticleId())
                .and("entryId").is(entry.getId()));
        //5. 根据行为实体 文章ID查询 是否点赞
        ApLikesBehavior one = mongoTemplate.findOne(apLikes, ApLikesBehavior.class);
        if (one != null) {
            map.put("islike", true);
        }
        //6. 根据行为实体 文章ID查询 是否不喜欢
        ApUnlikesBehavior unlikesBehavior = mongoTemplate.findOne(apLikes, ApUnlikesBehavior.class);
        if (unlikesBehavior != null) {
            map.put("isunlike", true);
        }
        //7. 根据行为实体 文章ID查询 是否收藏
        ApCollection collection = mongoTemplate.findOne(apLikes, ApCollection.class);
        if (collection != null) {
            map.put("iscollection", true);
        }
        //8. 根据登录用户id 去redis中查询是否关注该作者
        Double score = redisTemplate.opsForZSet().score(UserRelationConstants.FOLLOW_LIST + user.getId(), String.valueOf(dto.getAuthorApUserId()));
        //9. 封装结果 返回
        if (score != null) {
            map.put("isfollow", true);
        }
        return ResponseResult.okResult(map);

    }
}
